package ua.eug.robov2.screens;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import ua.eug.robov2.R0b0Game;
import ua.eug.robov2.menus.MenuClickListener;
import ua.eug.robov2.menus.SideMenu;
import ua.eug.robov2.schematic.Sheet;
import ua.eug.robov2.schematic.SheetActor;
import ua.eug.robov2.schematic.SheetPlacer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.Touchable;

public class SchScreen implements Screen, MenuClickListener {

	public static final String SaveFileName = "test.f";
	static final String[] menuItems = {"wrench", "trash", "maze", "tube"};
	static final String[] chipItems = {"chipand", "chipor", "chipnot", "chiprs"};
	static final String[] yesNoItems = {"undo", "yes", "no"};
	
	private R0b0Game r0b0;
	private Stage stage;
	private Sheet sheet;
	private SheetActor sa;
	private SheetPlacer placer;
	
	FileHandle file;
	
	public SchScreen (R0b0Game r0b0) {
		this.r0b0 = r0b0;
		stage = new Stage();
		
		SheetActor.LoadChipClass();
		
		Boolean ex = Gdx.files.local(SaveFileName).exists();
		
		if (ex) {
			FileInputStream fis;
			try {
				fis = new FileInputStream(SaveFileName);
				ObjectInputStream oin = new ObjectInputStream(fis);
				sa = (SheetActor) oin.readObject();
				oin.close();
			} catch (IOException | ClassNotFoundException e) {
				//e.printStackTrace();
				System.out.println("Wrong save version");
			}		
		} 
		
		if (sa == null) { // load failed
			sa = new SheetActor();
			sa.init();
		}
		
		sheet = sa.getSheet();
		sa.setTouchable(Touchable.enabled);
		stage.addActor(sa);
				
		placer = new SheetPlacer(sa);
		stage.addActor(placer); // chip placer, not visible by default
	}

	@Override
	public Boolean clicked (String item) {
		switch (item) {
			case "maze":
				r0b0.setMazeScreen();
				break;
			case "tube":
				SideMenu.getInstance().newMenu(chipItems, this);
				break;
			case "wrench" :
// !!!
				try {
					FileOutputStream f1 = new FileOutputStream("test.f");
					ObjectOutputStream oos = new ObjectOutputStream(f1);
					oos.writeObject(sa);
					oos.flush();
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

				break;
			case "chipand":
			case "chipor":
			case "chipnot": 
			case "chiprs":
				placer.showChip(item); 
				SideMenu.getInstance().newMenu(yesNoItems, this);
				break;
			case "yes":
				sa.addChip();
			case "no":
				placer.hideChip();
				SideMenu.getInstance().newMenu(menuItems, this);
				break;
			default:
		}
		return true;
	}
	
	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0.96f, 0.96f, 0.8f, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
		stage.draw();
	}

	@Override
	public void resize(int width, int height) {
		sa.resize(8);
	}

	@Override
	public void show() {
		Gdx.input.setInputProcessor(stage);
		SideMenu menu = SideMenu.getInstance();
		menu.newMenu(menuItems, this );		
		stage.addActor(menu);
	}

	@Override
	public void hide() {
		
	}

	@Override
	public void dispose() {
		stage.dispose();
	}
}
